<template>
  <div>
    <el-card class="box-card">
      <el-button @click="dialogVisible = true" > 添加分类 </el-button>

      <div>
        <el-table
          :data="tableData"
          style="width: 100%"
          border
          lazy
          row-key="cat_id"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column label="#"> </el-table-column>
          <el-table-column prop="cat_name" label="分类名字"> </el-table-column>
          <el-table-column prop="cat" label="是否有效">
            <i class="el-icon-success" style="color: red"> </i>
          </el-table-column>
          <el-table-column prop="cat_level" label="排序">
            <template v-slot="scope">
              <el-tag v-if="scope.row.cat_level == 0">标签一</el-tag>
              <el-tag type="success" v-if="scope.row.cat_level == 1"
                >标签二</el-tag
              >
              <el-tag type="danger" v-if="scope.row.cat_level == 2"
                >标签三</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作">
            <el-button> 编辑</el-button>
            <el-button> 删除</el-button>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="params.pagenum"
      :page-sizes="[5, 1, 300, 400]"
      :page-size="params.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <div>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <span>
          <!-- 添加 -->
          <div class="block">
            <el-input
              v-model="input.cat_name"
              placeholder="请输入内容"
            ></el-input>
            <el-cascader
              v-model="vvalue"
              :options="tableData"
              :props="categories"
              @change="handleChange"
            ></el-cascader>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="tjall">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <div class="block"></div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      currentPage4: 1,
      tableData: [],
      dialogVisible: false,
      vvalue: [],
      input: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0,
      },
      categories: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },

      params: {
      type: 1,
        pagenum: 1,
        pagesize: 1,
      },
      total: 0,
    };
  },

  created() {
    this.getgood();
  },

  methods: {
    // 这是获取父类id，和几级
    handleChange() {
      if (this.vvalue.length === 0) {
        (this.input.cat_pid = 0), (this.input.cat_level = 0);
      } else {
        console.log(this.vvalue);
        this.input.cat_pid = this.vvalue[this.vvalue.length - 1];
        this.input.cat_level = this.vvalue.length;
      }
    },

    // 点击确定添加
    async tjall() {
      console.log(this.input);
      const { data: res } = await this.axios.post("/categories", this.input);
      console.log(res);
      this.dialogVisible = false;
      this.getgood();
    },
    // 这是初始化渲染
    async getgood() {
      const { data: res } = await this.axios.get("/categories", this.params);
      console.log(res);
      this.tableData = res.data;
      this.total = res.data.length;
    },

    handleSizeChange(val) {
      this.params.pagesize = val;
      this.getgood();
    },
    handleCurrentChange(val) {
      this.params.pagenum = val;

      this.getgood();
    },
  },
};
</script>
<style lang="scss" scoped>
</style>